﻿package bdz.projects.lavender.doi.ui
{
	import bdz.projects.lavender.doi.utils.DOIAudio;
	import flash.display.MovieClip;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	
	import org.papervision3d.core.data.UserData;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	
	/**
	 * ...
	 * @author ...
	 */
	public dynamic class PaperMenuItem extends DisplayObject3D 
	{
		public var rendererWidth:Number;
		public var rendererHeight:Number;
		private var _plane:Plane;
		private var _mat:MovieMaterial;
		private var _imat:MovieMaterial;
		private var _imc:MovieClip;
		
		private var _clip:MovieClip;
		private var _defaultInformation:Object;
		private var _double:Boolean;
		private var _labelPlane:Plane;
		private var _labelClip:MovieClip;
		private var _labelMat:MovieMaterial;
		public var labelZ:Number = -40;
		private var _ixoffset:Number = 0;
		private var _label:String;
		private var _inbetween:Plane;
		private var _ishow:Boolean;
		private var hiddenZ:Number = -500;
		private var hiddenY:Number = 0;
		private var hiddenRotY:Number = 180;
		public var baseTransparency:Number = 1;
		
		function PaperMenuItem(_renderer:MovieClip, _lrenderer:MovieClip, _definfo:Object, double:Boolean = true, ixo:Number = 0, ishow:Boolean = true ) {
			
			super();
			_ixoffset = ixo;
			rendererWidth = _renderer.width;
			rendererHeight = _renderer.height;
			_defaultInformation = _definfo;
			
			_clip = _renderer;
			_clip.definfo = _defaultInformation;
			
			_labelClip = _lrenderer;
			_labelClip.definfo = _defaultInformation;
			
			_double = double;
			
			_ishow = ishow;
			
			renderPaper();
			
			
			
		}
		protected function renderPaper():void {
			_mat = new MovieMaterial(_clip, true, false, false);
			_clip.defmat = _mat;
			_mat.smooth = true;
			_mat.interactive = true;
			_mat.doubleSided = false;
			
			_mat.name = "base";
			_plane = new Plane(_mat, _clip.width, _clip.height, 1, 1);
			_plane.userData = new UserData({ movie:_clip, item:this });
			
			_clip.pmenu = this;
			
			
			addChild(_plane);
			
			
			_labelMat = new MovieMaterial(_labelClip, true, false, false);
			_labelClip.defmat = _labelMat;
			_labelMat.smooth = true;
			_labelMat.interactive = false;
			_labelMat.doubleSided = false;
			_label = _defaultInformation.name;
			_labelPlane = new Plane(_labelMat, _labelClip.width, _labelClip.height, 2, 2);
			_labelClip.pmenu = this;
			_labelPlane.z = -30;
			_labelPlane.x = _ixoffset;
			addChild(_labelPlane);
			
			if(!_ishow){
				z = hiddenZ;
				rotationY = hiddenRotY;
				y = hiddenY;
			}
			visible = _ishow;
			
			
		}
		public function createInBetween(mc:MovieClip, scale:Number = 1, speed:Number = .4):void {
			if(_inbetween == null){
				_imat = new MovieMaterial(mc, true, false, false);
				_imat.smooth = true;
				_imc = mc;
				_imat.doubleSided = false;
				_inbetween = new Plane(_imat, mc.width, mc.height, 1, 1);
				_inbetween.scale = 0;
				addChild(_inbetween);
				TweenMax.killTweensOf(_inbetween);
				TweenMax.to(_inbetween, speed, {z:-10, scale:1.1, ease:Strong.easeInOut } );
			}else{
				TweenMax.killTweensOf(_inbetween);
				TweenMax.to(_inbetween, speed, {z:-10, scale:1.1, ease:Strong.easeInOut } );
			}
		}
		private function clearInBetween():void {
			try {
				_imat.destroy();
				_imc = null;
				removeChild(_inbetween);
				_inbetween = null;
			}catch (e:Error) {
				trace("error clearing in between");
			}
		}
		public function hide(delay:Number = 0):void{
			
			TweenMax.killTweensOf(this);
			TweenMax.to(this, .8, {delay:delay, z:hiddenZ, rotationY:hiddenRotY, y:hiddenY, ease:Strong.easeOut, onComplete:function(){visible=false;}});
		}
		public function show(delay:Number = 0, oz:Number = 0):void{
			
			visible = true;
			_mat.interactive = true;
			TweenMax.killTweensOf(this);
			TweenMax.to(this, .8, {delay:delay, z:oz, rotationY:0, y:0, ease:Strong.easeOut});
		}
		public function removeInBetween():void {
			if (_inbetween) {
				TweenMax.killTweensOf(_inbetween);
				TweenMax.to(_inbetween, .4, { scale:0, ease:Strong.easeInOut, onComplete:clearInBetween } );
			}
		}
		public function cleanUp():void {
			try {
				_imat.destroy();
				_imc = null;
				removeChild(_inbetween);
			}catch (e:Error) {
				
			}
			
			_labelClip = null;
			_clip = null;
			_labelMat.destroy();
			_mat.destroy();
			removeChild(_plane);
			removeChild(_labelPlane);
		}
		public function deactivate():void {
			
			_labelMat.animated = false;
			_labelMat.movieTransparent = false;
			_labelMat.smooth = false;
			
			_mat.animated = false;
			_mat.movieTransparent = false;
			_mat.smooth = false;
			_mat.interactive = true;
			
		}
		public function select():void{
			_mat.interactive = false;
			
		}
		public function deselect():void{
			_mat.interactive = true;
		}
		public function activate():void {
			_labelMat.animated = true;
			_labelMat.movieTransparent = true;
			_labelMat.smooth = true;
			
			_mat.animated = true;
			_mat.movieTransparent = true;
			_mat.smooth = true;
			_mat.interactive = true;
			_plane.z = 0;
		}
		public function get defaultInformation():Object{
			return _defaultInformation;
		}
		public function get labelClipMaterial():MovieClip {
			return _labelClip;
		}
		public function get labelMaterial():MovieMaterial {
			return _labelMat;
		}
		public function get baseMaterial():MovieMaterial {
			return _mat;
		}
		public function get baseClipMaterial():MovieClip {
			return _clip;
		}
		public function get basePlaneMaterial():Plane {
			return _plane;
		}
		public function get labelPlaneMaterial():Plane {
			return _labelPlane;
		}
		public function get label():String {
			return _label;
		}
		
	}
	
}